package com.demo.stream;




import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamTest {

    List<Person> list = Arrays.asList(
            new Person("张三", 20, "一"),
            new Person("李四", 21, "二"),
            new Person("雪梅", 7, "三"),
            new Person("小米", 3, "一"),
            new Person("华为", 19, "二")
    );


    public void testa() {
        Person p = null;
        Person p1 = new Person("华为", 19, "二");

        Optional<Person> optional = Optional.ofNullable(p);
        if (optional.isPresent()) {
            System.out.println(optional.get());
        }
        Person person = optional.orElse(p1);
        Person person1 = optional.orElseGet(() -> p1);
        System.out.println(person);
        System.out.println(person1);

    }


    public void test1() {


        //总数
        Long collect = list.stream()
                .collect(Collectors.counting());
        System.out.println(collect);
        //年龄平均数
        list.stream()
                .collect(Collectors.averagingDouble((x) -> x.getAge()));
        //年龄总和
        Integer collect1 = list.stream()
                .collect(Collectors.summingInt((x) -> x.getAge()));
        //年龄最大员工
        Optional<Person> collect2 = list.stream()
                .collect(Collectors.maxBy((x, y) -> Integer.compare(x.getAge(), y.getAge())));
        System.out.println(collect2.get());
        //年龄最小的员工
        Optional<Person> collect3 = list.stream()
                .collect(Collectors.minBy((x, y) -> Integer.compare(x.getAge(), y.getAge())));
        System.out.println(collect3.get());

        //年龄最小
        Optional<Integer> collect4 = list.stream()
                .map((x) -> x.getAge())
                .collect(Collectors.minBy(Integer::compare));
        System.out.println(collect4.get());

        //班级分组
        Map<String, List<Person>> collect5 = list.stream()
                .collect(Collectors.groupingBy((x) -> x.getGrade()));
        //多级分组
        Map<String, Map<String, List<Person>>> collect6 = list.stream()
                .collect(Collectors.groupingBy(
                        (x) -> x.getGrade(),
                        Collectors.groupingBy((y) -> {
                            if (y.getAge() > 10) {
                                return "小学生";
                            } else {
                                return "初中生";
                            }
                        })
                ));

        System.out.println(collect6);



/*
        List<Person> collect3 = list.stream()
                .collect(Collectors.toList());

        Set<Person> collect4 = list.stream()
                .collect(Collectors.toSet());

        HashSet<Person> collect5 = list.stream()
                .collect(Collectors.toCollection(HashSet::new));*/


    }


    public void test2() {

    }


    public void test3() {

    }


    public void test4() {
        Stream<Integer> iterate = Stream.iterate(0, (x) -> x + 1);
        iterate.limit(10)
                .forEach(System.out::println);

        Stream<Integer> generate = Stream.generate(() -> new Random().nextInt(10));
        generate.limit(2)
                .forEach(System.out::println);
    }


    public void test5() {


        List<Student> studentList1 = Arrays.asList(
                new Student("张三", 1),
                new Student("李四", 2)

        );

        List<Student> studentList2 = Arrays.asList(
                new Student("雪梅", 3),
                new Student("小米", 38),
                new Student("华为", 19)
        );
        Grade grade1 = new Grade();
        grade1.setGrade("1");
        grade1.setStudents(studentList1);

        Grade grade2 = new Grade();
        grade2.setGrade("2");
        grade2.setStudents(studentList2);
        //班级集合
        List<Grade> listGrade = new ArrayList<>();
        listGrade.add(grade1);
        listGrade.add(grade2);

        listGrade.stream()
                .flatMap(g -> g.getStudents().stream())
                .map(s -> s.getAge())
                .forEach(System.out::println);

    }


    public void test6() {

        MyTest1 my = new MyTest1();
        my.tt();

    }

}
